<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Owner extends CI_Controller {


	public function __construct(){
		parent::__construct();
		$this->load->helper(array('form', 'url'));
		$this->load->model('member_model');      // 加载会员模型
		$this->load->model('feedback_model');    // 加载投诉建议模型
		$this->load->model('gzsub_model');     // 加载工长预约模型
	}


	/**
	 * [login description]
	 * @return [type] [description]
	 */
	public function login()
	{
		// 判断参数是否正确
		if(empty($_POST['username']) /*|| empty($_POST['system']) || empty($_POST['version'])*/){
			$this->sys_message->errormsg("抱歉,参数不能为空!");
		}

		// 根据用户名查询业主是否存在
		$member = $this->member_model->get_member_info_by_username($_POST['username']);
		if(!$member){
			$this->sys_message->errormsg("抱歉,该用户不存在!");
		}

		// 比对密码
		
		// 存入session中
		$_SESSION['member_id']     = $member['uid'];
		$_SESSION['member_mobile'] = $member['uname'];
			
		$reg['userId']   = $member['uid'];
		$reg['headUrl']  = $member['face'];
		$reg['sex'] 	 = $member['sex'];
		$reg['tel']		 = $member['mobile'];
		$reg['address']  = $member['address'];
		$this->sys_message->asuccess($reg);
	}


	/**
	 * [modifyInfo 个人资料修改]
	 * @return [type] [description]
	 */
	public function modifyInfo(){
		// 判断参数是否正确
		if(empty($_POST['userId']) || empty($_POST['name']) || empty($_POST['address']) || empty($_POST['sex']) ){
			$this->sys_message->errormsg("抱歉,参数不能为空!");
		}	

		// 根据id查询用户是否存在
		$member = $this->member_model->get_member_info_by_uid($_POST['userId']);
		if(!$member){
			$this->sys_message->errormsg("抱歉,该用户不存在!");
		}

		// 根据用户名查询业主是否存在
		$member = $this->member_model->get_member_info_by_username($_POST['username']);
		if($member){
			$this->sys_message->errormsg("抱歉,该用户名已存在,请重新命名!");
		}

		// 整理需要更新的数据
		$_update['uname']     = $_POST['name'];
		$_update['address']   = $_POST['address'];
		$_update['sex']       = $_POST['sex'];

		$uid = $_POST['userId'];
		if($this->member_model->update_member($uid,$_update)){
			$this->sys_message->success();
		}else{
			$this->sys_message->error();
		}
	}	

	/**
	 * [decorationRecords 装修信息查询 - 未完成]
	 * @return [type] [description]
	 */
	public function decorationRecords(){
		// 判断参数是否正确
		if(empty($_POST['userId'])){
			$this->sys_message->errormsg("抱歉,参数不能为空!");
		}

		// 根据id查询用户是否存在
		$member = $this->member_model->get_member_info_by_uid($_POST['userId']);
		if(!$member){
			$this->sys_message->errormsg("抱歉,该用户不存在!");
		}
	}

	/**
	 * [feedback 投诉建议]
	 * @return [type] [description]
	 */
	public function feedback(){
		// 判断参数是否正确
		if(empty($_POST['userId']) || empty($_POST['content'])){
			$this->sys_message->errormsg("抱歉,参数不能为空!");
		}

		// 根据id查询用户是否存在
		$member = $this->member_model->get_member_info_by_uid($_POST['userId']);
		if(!$member){
			$this->sys_message->errormsg("抱歉,该用户不存在!");
		}

		// 整理要添加的数据
		$_insert['uid'] 	= $_POST['userId'];
		$_insert['content'] = $_POST['content'];
		$_insert['date'] 	= time();


		if($this->feedback_model->insert($_insert)){ // 增加
			$this->sys_message->success();
		}else{
			$this->sys_message->error();
		}
	}

	/**
	 * [getcode 发送短信]
	 * @return [type] [description]
	 */
	public function getcode(){
		// 判断参数是否正确
		if(empty($_POST['phone']) || empty($_POST['type'])){
			$this->sys_message->errormsg("抱歉,参数不能为空!");
		}

		$mode='/^1[0-9]{10,10}$/'; // 手机号码匹配
    	if(!preg_match($mode,$_POST['phone'],$arr)){
        	
            $this->sys_message->errormsg("手机号码格式不正确,请重新输入!");
    	}

    	$mobile = $_POST['phone'];
		// 发送短信
		$this->load->library('mobile_sns',array('mobile'=>$mobile));   	
		$captcha = $this->mobile_sns->create_char();	// 验证码

		if($_POST['type'] == 0){ // 注册
			$reg = $this->mobile_sns->yzm('您的找回密码验证码是【'.$captcha.'】,请不要把验证码泄露给其他人！。如非本人操作，可不用理会！');

		}
		if($_POST['type'] == 1){ // 忘记密码
			$reg = $this->mobile_sns->yzm('您的注册验证码是【'.$captcha.'】,请不要把验证码泄露给其他人！。如非本人操作，可不用理会！');
		}

		if($reg == 'ok'){
			$this->sys_message->success();
		}else{
            $this->sys_message->errormsg($this->mobile_sns->error);
		}
	}

	/**
	 * [update 版本更新 - 未完成]
	 * @return [type] [description]
	 */
	public function update(){
		// 判断参数是否正确
		if(empty($_POST['version']) || empty($_POST['type']) || empty($_POST['imei'])){
			$this->sys_message->errormsg("抱歉,参数不能为空!");
		}
	}
	

	/**
	 * [reservations 我的预约]
	 * @return [type] [description]
	 */
	public function reservations(){
		/*// 判断参数是否正确
		if(empty($_POST['userId'])){
			$this->sys_message->errormsg("抱歉,参数不能为空!");
		}

		// 根据id查询用户是否存在
		$member = $this->member_model->get_member_info_by_uid($_POST['userId']);
		if(!$member){
			$this->sys_message->errormsg("抱歉,该用户不存在!");
		}*/

		// 获取预约数据
		$arr = $this->gzsub_model->get_member_yuyue(149);
		print_r($arr);
	}

	public function test(){
		$this->mb_model->get_test();
	}
}
